package aspirecn.project3.interceptor;


import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import aspirecn.project3.enums.BizCodeEnum;
import aspirecn.project3.model.LoginUser;
import aspirecn.project3.util.CommonUtil;
import aspirecn.project3.util.JWTUtil;
import aspirecn.project3.util.JsonData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//下述包新版本已废弃
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * packageName aspirecn.project3.interceptor
 *
 * @author 邵一宸
 * @version JDK 17
 * @className LoginInterceptor
 * @date 2024/7/19
 * @description TODO
 */

@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    public static ThreadLocal<LoginUser> threadLocal = new ThreadLocal<>();


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (HttpMethod.OPTIONS.toString().equalsIgnoreCase(request.getMethod())) {
            response.setStatus(HttpStatus.NO_CONTENT.value());
            return true;
        }

        String accessToken = request.getHeader("Authorization");
        if (StringUtils.isBlank(accessToken)) {
            accessToken = request.getParameter("token");
        }


        if (StringUtils.isNotBlank(accessToken)) {
            System.out.println("token:"+accessToken);
            Claims claims = JWTUtil.checkJWT(accessToken);
            System.out.println("claims:"+claims);
            if (claims == null) {
                //未登录
                CommonUtil.sendJsonMessage(response, JsonData.buildResult(BizCodeEnum.ACCOUNT_UNLOGIN));
                return false;
            }

            Long id = Long.parseLong(claims.get("id").toString());
            String type = (String) claims.get("type");
            String name = (String) claims.get("name");
            String sex = (String) claims.get("sex");
            String email = (String) claims.get("email");
            String phone = (String) claims.get("phone");
            String status = (String) claims.get("status");

            LoginUser loginUser = LoginUser.builder()
                    .id(id)
                    .type(type)
                    .name(name)
                    .sex(sex)
                    .email(email)
                    .phone(phone)
                    .status(status)
                    .build();
            log.info("当前登录的用户为:{}",loginUser);
            threadLocal.set(loginUser);
            return true;
        }

        CommonUtil.sendJsonMessage(response,JsonData.buildResult(BizCodeEnum.ACCOUNT_UNLOGIN));
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        threadLocal.remove();
    }
}
